<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .father{
            position: relative;
            left: 0;
            top: 0;
            overflow: hidden;
            width: 200px;
            height: 200px;
            background-color: pink;
            margin: 100px;
        }
        .son{
           width: 100px;
           height: 100px;
           background-color: purple;
           margin-left: 50px;
        }
        .w{
           width: 200px;
           height: 200px;
           background-color: #ff6700;
           margin: 0 auto 100px;
           padding: 10px;
           border: 15px solid red;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son"></div>
    </div>
    <div class="w">w</div>
    <script>
        //1.可以得到元素的偏移 位置， 返回的是不带单位的数值
        var father = document.querySelector('.father');
        console.log(father.offsetLeft);
        console.log(father.offsetTop);
        //子盒子以带有定位的父盒子为准，如果没有父亲或者父亲没有定位，则以body为准
        var son = document.querySelector('.son')
        console.log(son.offsetLeft);
        console.log(son.offsetTop);

        //2.可以得到元素的大小 宽度 和高度 是包含 padding + border + width
        var w = document.querySelector('.w');
        console.log('2宽度:' + w.offsetWidth); //width + border + padding = 250 不包含margin
        console.log("2高度:" + w.offsetHeight);

        //3.返回带有定位的父级，否则返回body
        console.log(son.offsetParent); //返回的是带有定位的父级，父级无定位，返回body
        console.log(son.parentNode);  //返回的是最近一级的父级，不管父级有无定位
    </script>
</body>
</html>